import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GUI extends JFrame {
	static JPanel leftMenu;
	static JPanel nameBar;
	static JFrame window;
	static JButton nameTime;
	static JPanel leftPanel;
	static JPanel centerPanel;
	static JPanel bottomPanel;
	final static Color teal = new Color(0, 153, 153);
	final static Color yellow = new Color(255, 204, 0);
	final static Color blue = new Color(4, 40, 220);
	int xSize;
	int ySize;
	String state;
	static int takeoutTable = 100;
	
	static int userID;
	static int userJobCode;
	static String userName;

	private static String item; // Used to identify the item the user clicked on
								// (Burger, Drink, etc)

	/**
	 * Constructor for the GUI object. Takes an ID and job code as input and
	 * creates the proper layout for each job role.
	 * 
	 * @param id
	 *            The id of the current user.
	 * @param jobCode
	 *            The job code of the current user. 0 = Hostess, 1 = Server, 2 =
	 *            Manager
	 * @param name
	 *            The last name of the current user.
	 */
	public GUI(int id, int jobCode, String name) {
		userID = id;
		userJobCode = jobCode;
		userName = name;
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		xSize = ((int) tk.getScreenSize().getWidth());
		initLeftMenu(500);
		GUI.nameBar = new JPanel();
		
		/****** Set up JFrame Window *******/
		window = new JFrame(); // create the window
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        window.setMaximizedBounds(ge.getMaximumWindowBounds());
        window.setExtendedState(window.getExtendedState() | window.MAXIMIZED_BOTH);
		//Used to remove the borders - user cannot minimize, maximize, or close the window
        window.setUndecorated(true);
		Color color = UIManager.getColor("activeCaptionBorder");
		window.getRootPane().setBorder(BorderFactory.createLineBorder(color, 4));
		window.setResizable(false);
		/****** End Set up JFrame Window *******/
		
		leftPanel = new JPanel();
		centerPanel = new JPanel();
		bottomPanel = new JPanel();

		nameTime = nameTime(userName, new Date());
		//window.setSize(xSize, ySize);
		window.setLayout(new BorderLayout());
		leftPanel.setLayout(new BorderLayout());
		centerPanel.setLayout(new GridLayout(4, 4));
		centerPanel.setBackground(teal);
		nameBar.setLayout(new GridLayout(1, 1));
		nameBar.add(nameTime);
		nameBar.setPreferredSize(new Dimension(500, 150));
		nameBar.setMinimumSize(new Dimension(500, 150));

		if (userJobCode == 1)
			Server.server(userID);
		else if (userJobCode == 0)
			Hostess.hostess(userID);
		else if (userJobCode == 2)
			Manager.manager(userID);
		else
			JOptionPane.showMessageDialog(null, "Position Error.", null, 0);

	}

	static void windowDone() {
		leftPanel.add(nameBar, BorderLayout.PAGE_START);
		leftPanel.add(leftMenu);
		window.add(leftPanel, BorderLayout.LINE_START);
		window.add(bottomPanel, BorderLayout.PAGE_END);
		window.add(centerPanel, BorderLayout.CENTER);
		
		window.setVisible(true);
	}

	static JButton takeoutButton(int font, int id) {
		JButton takeout = newButton("Takeout", font);
		takeout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int customers = PopUp.getNumCustomers();// This should really be
														// called from the
														// ActionListener
				if (customers > -1) {
					Server.serverAtTable(takeoutTable++, Server.menuTypes, id,
							customers);
				}
				// Server.serverAtTable(takeoutTable++, Server.menuTypes, id);
				// Original code
			}

		});
		return takeout;
	}

	static void initLeftMenu(int size) {
		leftMenu = new JPanel();
		leftMenu.setLayout(new GridLayout(3, 3));
		leftMenu.setSize(size, size);
	}

	JButton nameTime(String name, Date time) {
		JButton nameTime = new JButton();
		DateFormat dateFormat = new SimpleDateFormat("HH:mm");
		nameTime.setText("<html>" + name + "<br />" + dateFormat.format(time)
				+ "</html>");
		nameTime.setFont(new Font("Arial", Font.PLAIN, 40));
		nameTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (name.equals("Mike")) {
					// tipAmount obtains the cash tip amount being claimed
					// 0.00 if no cash tips were claimed
					// empty string if user cancels
					String tipAmount = PopUp.runServerClockOut();
					while (tipAmount.equals("CLEAR")) { // User clears the cash
														// tips display
						tipAmount = PopUp.runServerClockOut();
					}
					if (tipAmount.equals("")) {
						// do nothing - user cancelled
					} else {

						closeFrame();
						new StartSystem();
					}
				} else {
					// if out = 0 then employee has clocked out.
					// if out = 1 then employee selected cancel and is still
					// clocked in
					int out = PopUp.runClockOut();
					if (out == 0) {

						closeFrame();
						new StartSystem();
					} else {
						// do nothing
					}
				}
			}
		});
		return nameTime;
	}

	public static JPanel exitOnly() {
		JPanel exitPanel = new JPanel();
		JButton exitButton = newButton("Exit", 40);
		exitPanel.setBackground(teal);
		exitButton.setPreferredSize(new Dimension(600, 75));
		exitPanel.setLayout(new FlowLayout());
		exitPanel.add(exitButton);
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeFrame();
				new StartSystem();
			}

		});
		return exitPanel;
	}

	static JPanel tableBottomPanel(int id) {
		JPanel sendPanel = new JPanel();
		sendPanel.setBackground(teal);
		sendPanel.setLayout(new FlowLayout());

		JButton sendButton = newButton("Send Order", 40);
		sendButton.setPreferredSize(new Dimension(600, 75));
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*
				 * Add code to send the queued order to the database
				 */

				closeFrame();
				new GUI(userID, userJobCode, userName);
			}
		});

		JButton cancelButton = newButton("Cancel", 40);
		cancelButton.setPreferredSize(new Dimension(600, 75));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeFrame();
				new GUI(userID, userJobCode, userName);
			}
		});

		sendPanel.add(sendButton);
		sendPanel.add(cancelButton);
		return sendPanel;
	}

	static void resetPanels() {
		leftMenu.removeAll();
		bottomPanel.removeAll();
		centerPanel.removeAll();
		// leftPanel.removeAll();
		// TODO Auto-generated method stub

	}

	public static JButton newButton(String input, int size) {
		JButton temp = new JButton(input);
		temp.setFont(new Font("Arial", Font.PLAIN, size));

		return temp;
	}

	static JButton emptyButton() {
		JButton temp = new JButton("");
		return temp;
	}

	static JButton menuButton(String input, int size) {
		JButton temp = new JButton(input);
		temp.setFont(new Font("Arial", Font.PLAIN, size));
		temp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Makes database call here
				// Checks to ensure that a seat has been selected then passes
				// the item to set the Ticket Order
				if (TicketGrid.getIsHeaderClicked() == true) {
					item = temp.getText();
					TicketGrid.setTicketOrder(item);
				}
			}
		});

		return temp;
	}

	public static void main(String[] args) {
		int id = 1113;
		//GUI login = new GUI(userID, userJobCode, userName);
		// login.setSize(800,600);
	}

	public static JPanel mainMenuBar(int empID2) {
		JPanel mMPanel = new JPanel();
		mMPanel.setBackground(teal);
		mMPanel.setLayout(new FlowLayout());

		JButton cancelButton = newButton("Main Menu", 40);
		cancelButton.setPreferredSize(new Dimension(600, 75));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Grids.setHighLight();
				closeFrame();
				new GUI(userID, userJobCode, userName);
			}
		});

		mMPanel.add(cancelButton);
		return mMPanel;
	}
	
	/**
	 * Closes the GUI JFrame window
	 */
	public static void closeFrame(){
		window.dispose();
	}//end closeFrame method
	

	public static JButton setSectionButton(int empID) {
		JButton sections=newButton("Set Sections", 40);
		boolean using;
		/*if(sections.getBackground()!=null)
			using=true;
		else
			using=false;*/
		sections.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(sections.getBackground()!=(new JButton().getBackground())){
					closeFrame();
					new GUI(empID, userJobCode, userName);
				}
				else{
					sections.setBackground(Color.DARK_GRAY);
					Server.setSections(empID);
				}
				}
					
			});
		// TODO Auto-generated method stub
		return sections;
	}

	public static void reset(){
		closeFrame();
		new GUI(userID, userJobCode, userName);
	}
}
